﻿CREATE PROCEDURE [util].[GetErrorOutput]
@Output NVARCHAR (MAX)=NULL OUTPUT
AS
BEGIN
		DECLARE @ErrorNumber int, @ErrorSeverity int, @ErrorState int, @ErrorProcedure as nvarchar(128), @ErrorLine int, @ErrorMessage nvarchar(max);
		SELECT 
			@ErrorNumber = ERROR_NUMBER()
			,@ErrorSeverity = ERROR_SEVERITY()
			,@ErrorState = ERROR_STATE()
			,@ErrorProcedure = ERROR_PROCEDURE()
			,@ErrorLine = ERROR_LINE()
			,@ErrorMessage = ERROR_MESSAGE();

		SET	@Output = 
			N'Error found in ' 
			+ ISNULL(ERROR_PROCEDURE() ,'script')
			+ N' on line ' 
			+ CAST(ERROR_LINE() AS nvarchar) 
			+ N':'
			+ CHAR(13)
			+ ERROR_MESSAGE();
END;